package com.jushu.storbox.util.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.ListView;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.util.OnImageDownload;
import com.jushu.storbox.util.PublicTools;

public class IconVerification {
	private static ImageDownloader mDownloader;
	
	public static void fileIconClass(Context context, ListView listView,
			ImageView imageView, int localityOrService, String fileName,
			boolean isDownloadIcon, String iConUrl, String thumbnailURL) {
		if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingImage))) {
			if (!isDownloadIcon) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.image));
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingWebText))) {
			if (!isDownloadIcon) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.default_file_icon));
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingPackage))) {
			if (!isDownloadIcon) {
				if ((fileName.toLowerCase().endsWith(".rar"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.rar));
				} else if ((fileName.toLowerCase().endsWith(".zip"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.rar));
				} else {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.rar));
				}
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingAudio))) {
			if (!isDownloadIcon) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.music));
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingVideo))) {
			if (!isDownloadIcon) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.video));
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileDocments))) {
			if (!isDownloadIcon) {
				if ((fileName.toLowerCase().endsWith(".doc"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.doc));
				} else if ((fileName.toLowerCase().endsWith(".docx"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.doc));
				} else if ((fileName.toLowerCase().endsWith(".xls"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.excel));
				} else if ((fileName.toLowerCase().endsWith(".xlsx"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.excel));
				} else if ((fileName.toLowerCase().endsWith(".ppt"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.ppt));
				} else if ((fileName.toLowerCase().endsWith(".pptx"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.ppt));
				} else if ((fileName.toLowerCase().endsWith(".pdf"))) {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.pdf));
				} else {
					imageView.setImageBitmap(BitmapFactory.decodeResource(
							context.getResources(), R.mipmap.txt));
				}
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		} else {
			if (!isDownloadIcon) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.default_file_icon));
			} else {
				getImageData(context, listView, imageView, localityOrService,
						iConUrl, thumbnailURL);
			}
		}
	}
	
	/**
	 * getTypeImgeID
	 * */
	public static int getTypeImgeID(String fileName) {
		int typeImgResId = R.mipmap.default_file_icon;
		if (PublicTools.isFileType(fileName,
				Constant.WORD_TYPE)) {
			typeImgResId = R.mipmap.doc;
		} else if (PublicTools.isFileType(fileName,
				Constant.PPT_TYPE)) {
			typeImgResId = R.mipmap.ppt;
		} else if (PublicTools.isFileType(fileName,
				Constant.IMAGE_TYPE)) {
			typeImgResId = -1;
		} else if (PublicTools.isFileType(fileName,
				Constant.RAR_TYPE)) {
			typeImgResId = R.mipmap.rar;
		} else if (PublicTools.isFileType(fileName,
				Constant.ZIP_TYPE)) {
			typeImgResId = R.mipmap.rar;
		} else if (PublicTools.isFileType(fileName,
				Constant.VIDEO_TYPE)) {
			typeImgResId = R.mipmap.video;
		} else if (PublicTools.isFileType(fileName,
				Constant.MUSIC_TYPE)) {
			typeImgResId = R.mipmap.music;
		} else if (PublicTools.isFileType(fileName,
				Constant.TXT_TYPE)) {
			typeImgResId = R.mipmap.txt;
		} else if (PublicTools.isFileType(fileName,
				Constant.PDF_TYPE)) {
			typeImgResId = R.mipmap.pdf;
		} else if (PublicTools.isFileType(fileName,
				Constant.EXCEL_TYPE)) {
			typeImgResId = R.mipmap.excel;
		} else {
			typeImgResId = R.mipmap.default_file_icon;
		}
		return typeImgResId;
	}
	
	
	@SuppressWarnings("static-access")
	public static void fileIcon(Context context, ImageView imageView,
			String filePath, String fileName) {
		if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingImage))) {
//			AsyncViewTask asyncViewTask = new AsyncViewTask();
//			asyncViewTask.execute(context, imageView,filePath);
			Bitmap bmp = BitmapUtil.createImageThumbnail(filePath, PublicTools.getWindowWidth(context), PublicTools.getWindowHeight(context));
			imageView.setImageBitmap(bmp);
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingWebText))) {
			imageView.setImageBitmap(BitmapFactory.decodeResource(
					context.getResources(), R.mipmap.default_file_icon));
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingPackage))) {
			if ((fileName.toLowerCase().endsWith(".rar"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.rar));
			} else if ((fileName.toLowerCase().endsWith(".zip"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.rar));
			} else {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.rar));
			}
		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingAudio))) {
			imageView.setImageBitmap(BitmapFactory.decodeResource(
					context.getResources(), R.mipmap.music));

		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileEndingVideo))) {
			imageView.setImageBitmap(BitmapFactory.decodeResource(
					context.getResources(), R.mipmap.video));

		} else if (checkEndsWithInStringArray(fileName, context.getResources()
				.getStringArray(R.array.fileDocments))) {
			if ((fileName.toLowerCase().endsWith(".doc"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.doc));
			} else if ((fileName.toLowerCase().endsWith(".docx"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.doc));
			} else if ((fileName.toLowerCase().endsWith(".xls"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.excel));
			} else if ((fileName.toLowerCase().endsWith(".xlsx"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.excel));
			} else if ((fileName.toLowerCase().endsWith(".ppt"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.ppt));
			} else if ((fileName.toLowerCase().endsWith(".pptx"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.ppt));
			} else if ((fileName.toLowerCase().endsWith(".pdf"))) {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.pdf));
			} else {
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						context.getResources(), R.mipmap.txt));
			}
		} else {
			imageView.setImageBitmap(BitmapFactory.decodeResource(
					context.getResources(), R.mipmap.default_file_icon));
		}
	}
	/*
	private static class AsyncViewTask extends AsyncTask<Object, Void, Bitmap> {
		private ImageView image;
		private Context context;
		@Override
		protected Bitmap doInBackground(Object... params) {
			context = (Context) params[0];
			image = (ImageView) params[1];
			String imageURL = (String) params[2];
			Bitmap bitmap = loadImage(context, imageURL);
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result)
		{
			if (null != result)
				image.setImageBitmap(result);
			else 
				image.setBackgroundResource(R.mipmap.default_file_icon);
		}
	}
	private static Bitmap loadImage(Context context, String url)
	{
		Bitmap bitmap = null;
//		InputStream inputStream = null;
		try {
//			inputStream = new FileInputStream(new File(url));
			bitmap = BitmapUtil.createImageThumbnail(url, PublicTools.getWindowWidth(context), PublicTools.getWindowHeight(context));
		} catch (Exception e) {
		}
		return bitmap;
	}*/

	public static boolean checkEndsWithInStringArray(String checkItsEnd,
			String[] fileEndings) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.toLowerCase().endsWith(aEnd))
				return true;
		}
		return false;
	}

	private static void getImageData(Context context, final ListView listView,
			ImageView imageView, int localityOrService, String url,
			String thumbnailURL) {
		if (mDownloader == null) {
			mDownloader = new ImageDownloader();
		}
		if (mDownloader != null) {
			mDownloader.imageDownload(url, imageView, thumbnailURL,
					localityOrService,
					"/storage/emulated/0/CloudDrive/thumbnail", context,
					new OnImageDownload() {
						@Override
						public void onDownloadSucc(Bitmap bitmap, String c_url,
								ImageView mimageView) {
							ImageView imageView = (ImageView) listView
									.findViewWithTag(c_url);
							if (imageView != null) {
								imageView.setImageBitmap(bitmap);
								imageView.setTag("");
							}
						}
					});
		}
	}
}
